<?php

/**
 * ArticleModel.php 注释
 * 
 * @package 
 * @subpackage 
 * @author rendong01 rendong237@126.com
 * 
 * : tool.php 6 2012-07-28 15:01:50Z rendong237@gmail.com $
 */
class ArticleModel{
	/**
	 * 数据库对象
	 *
	 * @var Mysql
	 */
	private $db;
	
	/**
	 * 构造方法
	 *
	 * @param Mysql $db
	 */
	public function __construct($db)
	{
		$this->db = $db;
	}
	
	/**
	 * 添加
	 */
	public function add( array $value )
	{
		$v = array();
//		isset($value["aid"]) && $v["aid"] = (int)$value["aid"];
		isset($value["cid"]) && $v["cid"] = (int)$value["cid"];
		isset($value["author"]) && $v["author"] = $value["author"];
		isset($value["afrom"]) && $v["afrom"] = $value["afrom"];
		isset($value["keywords"]) && $v["keywords"] = $value["keywords"];
		isset($value["typename"]) && $v["typename"] = $value["typename"];
		isset($value["image"]) && $v["image"] = $value["image"];
		isset($value["image2"]) && $v["image2"] = $value["image2"];
		isset($value["pdf"]) && $v["pdf"] = $value["pdf"];
		isset($value["btnpdf"]) && $v["btnpdf"] = (int)$value["btnpdf"];
		isset($value["title"]) && $v["title"] = $value["title"];
		isset($value["subtitle"]) && $v["subtitle"] = $value["subtitle"];
		isset($value["intro"]) && $v["intro"] = $value["intro"];
		isset($value["recommend"]) && $v["recommend"] = $value["recommend"];
		isset($value["recseq"]) && $v["recseq"] = $value["recseq"];
		isset($value["content"]) && $v["content"] = $value["content"];
		isset($value["pubtime"]) && $v["pubtime"] = $value["pubtime"];
		isset($value["seq"]) && $v["seq"] = (int)$value["seq"];
		
		$arr_info = AdminUser::getLoginInfo();
		$v['adminid'] = $arr_info['id'];
		
		isset($value["syncaid"]) && $v["syncaid"] = (int)$value["syncaid"];
		
		return $this->db->insert("article", $v, true);
	}
	
	/**
	 * 修改
	 */
	public function change($aid, array $value)
	{
		$v = array();
		isset($value["cid"]) && $v["cid"] = (int)$value["cid"];
		isset($value["author"]) && $v["author"] = $value["author"];
		isset($value["afrom"]) && $v["afrom"] = $value["afrom"];
		isset($value["keywords"]) && $v["keywords"] = $value["keywords"];
		isset($value["typename"]) && $v["typename"] = $value["typename"];
		isset($value["image"]) && $v["image"] = $value["image"];
		isset($value["image2"]) && $v["image2"] = $value["image2"];
		isset($value["pdf"]) && $v["pdf"] = $value["pdf"];
		isset($value["btnpdf"]) && $v["btnpdf"] = (int)$value["btnpdf"];
		isset($value["title"]) && $v["title"] = $value["title"];
		isset($value["subtitle"]) && $v["subtitle"] = $value["subtitle"];
		isset($value["intro"]) && $v["intro"] = $value["intro"];
		isset($value["recommend"]) && $v["recommend"] = $value["recommend"];
		isset($value["recseq"]) && $v["recseq"] = $value["recseq"];
		isset($value["content"]) && $v["content"] = $value["content"];
		isset($value["pubtime"]) && $v["pubtime"] = $value["pubtime"];
		isset($value["seq"]) && $v["seq"] = (int)$value["seq"];
		isset($value["adminid"]) && $v["adminid"] = (int)$value["adminid"];
		isset($value["syncaid"]) && $v["syncaid"] = (int)$value["syncaid"];

		$this->db->update("article", $v, 'aid=' . (int)$aid);
	}
	
	/**
	 * 删除
	 */
	public function del($aid)
	{
		$this->db->delete("article", 'aid=' .  (int)$aid);
	}
	
	/**
	 * 读取列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function fetchAll($cid, $nowp, $per_page, $where = "")
	{
		if (empty($where))
			$where = " where cid=" . $cid;
			
		$totalNum = $this->db->fetchOne ('select count(*) from article ' . $where);
        
        require_once APP . '/application/models/Util.php';
        
        $pageInfo = Util::page ($totalNum, $per_page, $nowp);
        $start    = $pageInfo ['start'];
        $list     = $this->db->fetchAll ('select t1.*,t2.username from article t1
        left join admin t2 on t2.id=t1.adminid
        ' . $where .' order by seq desc,aid desc limit ' . $start . ',' . $per_page);
        
        return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	/**
	 * 显示
	 */
	public function show($aid)
	{
		return $this->db->fetchRow("select * from `article` where aid=\"?\"", array($aid));
	}
	
	/**
	 * 获取上一篇，下一篇
	 *
	 * @param unknown_type $aid
	 * @param unknown_type $cid
	 */
	public function getPreNext($baseUrl, $aid, $cid)
	{
		$pre = $this->db->fetchRow("select aid, title from article where cid=$cid and aid<$aid order by seq desc, aid desc limit 1");
		$next = $this->db->fetchRow("select aid, title from article where cid=$cid and aid>$aid order by seq asc, aid asc  limit 1");
		
		$pre_url = "#empty";
		$pre_title = _("article_my");
		if (!empty($pre)){
			$pre_url = 	$baseUrl . '/index.php?c=news&a=detail&aid=' . $pre['aid'];
			$pre_title = $pre['title'];
		}
		
		$next_url = "#empty";
		$next_title = _("article_my");
		if (!empty($next)){
			$next_url = 	$baseUrl . '/index.php?c=news&a=detail&aid=' . $next['aid'];
			$next_title = $next['title'];
		}
		
		$str_pre = _("article_pre");
		$str_next = _("article_next");
		$html = <<<EOT
<div class="detail_up_down_link">
	<div class="link_l f_l"><a href="$pre_url">{$str_pre}：$pre_title</a></div>
	<div class="link_r f_r"><a href="$next_url">{$str_next}：$next_title</a></div>
	<div class="clear"></div>
</div>
EOT;
		return $html;
	}
	
	/**
	 * 获取相关文章
	 *
	 * @param unknown_type $baseUrl
	 * @param unknown_type $aid
	 * @param unknown_type $cid
	 */
	public function getRelations($baseUrl, $content)
	{
		$aid = $content['aid'];
		$cid = $content['cid'];
		
		$keywords = $content['keywords'];
		$keywords = str_replace(array("，", " ", "　", '"', "'"), ",", $keywords);
		$arr_keys = explode(",", $keywords);
		$where = "";
		$or = "";
		foreach ($arr_keys as $v){
			$v = trim($v);
			if (!empty($v)){
				$where .= $or . ' title like "%' . $v . '%" or `keywords` like "%' . $v . '%"';
				$or = " or ";
			}
		}
		
		if (empty($where))
			$list = array();
		else	
			$list = $this->db->fetchAll("select aid,title from article where cid=$cid and aid!=$aid and ($where) limit 8");

		$article_relation = _("article_relation");
		$html = '<div class="list_content">
                	<h3 class="h3_title">'.$article_relation.'</h3>
                    <ul class="ul_list_a_26 clear_box detail_more_ul">';
		foreach ($list as $v){
			$url = $baseUrl . '/index.php?c=news&a=detail&aid=' . $v['aid'];
			$html.='<li><a href="' . $url . '" title="' . $v['title'] . '"><strong>·</strong> ' . $v['title'] . '</a></li>';
		}
		
		$html .= '</ul></div>';
		
		return $html;
	}
}